package com.fenghongzhang.day016;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * 播放音乐
 */
public class MusicService extends Service {

    private static final String TAG = "MusicService";
    private MediaPlayer mediaPlayer;

    private int index;

    private int playMode = 0;

    //数据源
    private List<Music> list;

    public MusicService() {
    }

    public class MyBinder extends Binder {
        //取到服务.
        public MusicService getService() {
            return MusicService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化音乐播放器
        mediaPlayer = new MediaPlayer();
        //初始化数据
        list = MusicUtil.getMusic(this);

        //动态注册广播接收者
        MyBroadcast myBroadcast = new MyBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConst.PAUSE);
        registerReceiver(myBroadcast,intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        //远程布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notify_ayout);

        builder.setCustomContentView(remoteViews);

        Notification build = builder.build();

        //相同id,代表更新
        startForeground(1,build);


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    //暂停
    public void pause() {
        mediaPlayer.pause();
    }

    //继续播放
    public void restart() {
        mediaPlayer.start();
    }

    //停止
    public void bbbbbb() {
        mediaPlayer.stop();
    }


    //最大值
    public int getMax() {
        //是否正在播放
        return mediaPlayer.getDuration();
    }

    //seekbar设置进度
    public void setProgress(int Progress) {
        mediaPlayer.seekTo(Progress);
    }

    //当前播放进度值
    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }


    //广播接收者
    class MyBroadcast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals(BroadcastConst.PAUSE)){
                mediaPlayer.pause();
            }
        }
    }

    //播放
    public void play(int position) {

        //发送更新的通知.
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        //加载布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notify_ayout);

        //第一个参数是id, 第二个参数是值.
        remoteViews.setTextViewText(R.id.notify_title,list.get(position).getTitle());

        //点击事件
        /**
         * PendingIntent 跳转 -> 发送一个广播. -> 广播接收者,-> 根据频道不同,做不同的逻辑
         *
         */
        Intent intent = new Intent();
        intent.setAction(BroadcastConst.PAUSE);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.notify_pause,broadcast);




        builder.setCustomContentView(remoteViews);


        Notification build = builder.build();

        //相同id,代表更新
        startForeground(1,build);



        index = position;
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(list.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });

            //播放完成要做的事情.
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    //伪代码
//                    int num = 3
//                    if(playMode){  0
//                        index = 随机
//                    }else if (是单曲){  1
//                        index = index
//                    }else if (是顺序){  2
//                        next();
//                    }
                    //修改播放模式
                    playLogic();


                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //播放逻辑
    private void playLogic() {
        switch (playMode) {
            case MusicUtil.MODE_ORDER:
                if (++index > list.size() - 1) {
                    index = 0;
                }
                play(index);
                Log.i(TAG, "onCompletion: " + index);
                break;
            case MusicUtil.MODE_RANDOM:
                index = new Random().nextInt(list.size());
                play(index);
                break;
            case MusicUtil.MODE_SINGLE:
                play(index);
                break;
        }
    }

    //下一首
    public void next() {
        playLogic();
    }




    //設置播放模式
    public void setPlayMode(int mode){
        playMode = mode;
        Log.i(TAG, "setPlayMode: "+index);
    }

    //上一首
    public void pre() {
        if (--index <= 0) {
            index = 0;
        }
        play(index);
    }
}
